package com.yxx.service.Impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.yxx.entity.Carousel;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.query.CarouselQuery;
import com.yxx.entityModel.vo.request.CarouselRequest;
import com.yxx.entityModel.vo.request.CarouselStatusRequest;
import com.yxx.entityModel.vo.response.CarouselBackResponse;
import com.yxx.entityModel.vo.response.CarouselResponse;
import com.yxx.enums.FilePathEnum;
import com.yxx.mapper.CarouselMapper;
import com.yxx.service.BlogFileService;
import com.yxx.service.CarouselService;
import com.yxx.strategy.context.UploadStrategyContext;
import com.yxx.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图服务层
 * @author yuxiangxun
 * @date 2025/2/1
 * @apiNote
 */
@Service
@Slf4j
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    @Resource
    private UploadStrategyContext uploadStrategyContext;

    @Resource
    private BlogFileService blogFileService;

    /**
     * 查看轮播图列表
     * @return
     */
    @Override
    public List<CarouselResponse> getCarouselList() {
        return carouselMapper.selectCarouselList();
    }

    /**
     * 查看轮播图列表
     * @param carouselQuery
     * @return
     */
    @Override
    public PageResult<CarouselBackResponse> getCarouselListVO(CarouselQuery carouselQuery) {
        // 查询轮播图的数量
        Long count = carouselMapper.selectCount(null);
        // 如果轮播图的数量为0，则new一个分页结果对象并返回
        if (count == 0){
            return new PageResult<>();
        }
        // 如果轮播图的数量不为0，则分页查询轮播图列表信息并返回
        List<CarouselBackResponse> carouselBackResponseList = carouselMapper.selectBackCarouselListVO(carouselQuery);
        return new PageResult<>(carouselBackResponseList,count);
    }

    /**
     * 上传轮播图
     * @param file
     * @return
     */
    @Override
    public String uploadCarousel(MultipartFile file) {
        // 上传文件
        // 根据文件上传的策略模式，选择不同的上传策略，并返回文件路径
        String url = uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.CAROUSEL.getPath());
        blogFileService.saveBlogFile(file,url,FilePathEnum.CAROUSEL.getFilePath());
        return url;
    }

    /**
     * 添加轮播图
     * @param carouselRequest
     */
    @Override
    public void addCarousel(CarouselRequest carouselRequest) {
        // 对象转换
        Carousel carousel = BeanCopyUtils.copyBean(carouselRequest, Carousel.class);
        // 添加轮播图到数据库
        carouselMapper.insert(carousel);
    }

    /**
     * 修改轮播图
     * @param carouselRequest
     */
    @Override
    public void updateCarousel(CarouselRequest carouselRequest) {
        // 断言，判断 carouselRequest.getId()是否为空，为空不执行下面的语句，则是抛出异常轮播图id不能为空
        Assert.notNull(carouselRequest.getId(),"轮播图id不能为空");
        // 对象转换
        Carousel carousel = BeanCopyUtils.copyBean(carouselRequest, Carousel.class);
        // 更新轮播图到数据库
        carouselMapper.updateById(carousel);
    }

    /**
     * 删除轮播图
     * @param carouselIdList
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean removeByIds(List<Integer> carouselIdList) {
        if (CollectionUtils.isEmpty(carouselIdList)) {
            return false;
        } else {
            TableInfo tableInfo = TableInfoHelper.getTableInfo(this.getEntityClass());
            return tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill() ? this.removeBatchByIds(carouselIdList, true) : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(carouselIdList));
        }
    }

    /**
     * 修改轮播图状态
     * @param carouselStatusRequest
     */
    @Override
    public void updateCarouselStatus(CarouselStatusRequest carouselStatusRequest) {
        Carousel carousel = Carousel.builder()
                .id(carouselStatusRequest.getId())
                .status(carouselStatusRequest.getStatus())
                .build();
        carouselMapper.updateById(carousel);
    }


}
